﻿Imports DNP.GestionComercial.FormBase
Imports DNP.GestionComercial.BE
Imports DNP.GestionComercial.BL

Public Class frmOrdenCompra
    Inherits frmFormBasePrincipal

#Region "Declaracion de Variables"

    Const usuario As String = "DLATORRE"

#End Region

#Region "Iniciar Pagina"

    Private Sub frmOrdenCompra_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        CargarFormulario()
        FormatGrid()

        grdLista.DataSource = Listar()

        ObtenerProducto(5)

    End Sub

#End Region

#Region "Funciones"

    Private Sub CargarFormulario()

        ListarParametricaXPadre(1)
        lblTitulo.Text = "Administración de Ordenes de Compras"

    End Sub

    Private Sub FormatGrid()

        grdLista.AutoGenerateColumns = False
        grdLista.Columns.Clear()
        grdLista.Columns.Add("grdOrdenCompra", "OrdenCompra")
        grdLista.Columns.Add("grdItem", "Item")
        grdLista.Columns.Add("grdCodigoProducto", "Código")
        grdLista.Columns.Add("grdProducto", "Producto")
        grdLista.Columns.Add("grdUnidadMedida", "Unidad Medida")
        grdLista.Columns.Add("grdCantidad", "Cantidad")
        grdLista.Columns.Add("grdPrecio", "Precio Unitario")
        grdLista.Columns.Add("grdDescuento", "Descuento")
        grdLista.Columns.Add("grdSubTotal", "Sub Total")

        grdLista.Columns(0).DataPropertyName = "grdOrdenCompra"
        grdLista.Columns(1).DataPropertyName = "grdItem"
        grdLista.Columns(2).DataPropertyName = "grdCodigo"
        grdLista.Columns(3).DataPropertyName = "grdProducto"
        grdLista.Columns(4).DataPropertyName = "grdUnidadMedida"
        grdLista.Columns(5).DataPropertyName = "grdPrecio"
        grdLista.Columns(6).DataPropertyName = "grdCantidad"
        grdLista.Columns(7).DataPropertyName = "grdDescuento"
        grdLista.Columns(8).DataPropertyName = "grdSubTotal"

        grdLista.Columns(0).Visible = False
        grdLista.Columns(1).Width = 50
        grdLista.Columns(2).Width = 120
        grdLista.Columns(3).Width = 250
        grdLista.Columns(4).Width = 200
        grdLista.Columns(5).Width = 80
        grdLista.Columns(6).Width = 80
        grdLista.Columns(7).Width = 80
        grdLista.Columns(8).Width = 120

        grdLista.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        grdLista.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        grdLista.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
        grdLista.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
        grdLista.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
        grdLista.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdLista.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdLista.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdLista.Columns(8).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

        grdLista.Columns(0).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        grdLista.Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        grdLista.Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        grdLista.Columns(3).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        grdLista.Columns(4).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        grdLista.Columns(5).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        grdLista.Columns(6).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        grdLista.Columns(7).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        grdLista.Columns(8).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

        'Dim cmbProducto As New DataGridViewComboBoxColumn
        'cmbProducto.ValueMember = "Codigo"
        'cmbProducto.DisplayMember = "Descripcion"
        'grdLista.Columns.Add(cmbProducto)

        'Dim cmbUnidadMedidad As New DataGridViewComboBoxColumn
        'cmbUnidadMedidad.ValueMember = "Codigo"
        'cmbUnidadMedidad.DisplayMember = "Descripcion"
        'grdLista.Columns.Add(cmbUnidadMedidad)

    End Sub

    Private Function Listar() As List(Of OrdenCompraDetalleBE)

        Dim lst As New List(Of OrdenCompraDetalleBE)

        If Not String.IsNullOrEmpty(txtOrdenCompra.Text) Then

        Else

            Dim filas As Integer = 30
            Dim i As Integer = 0

            For i = 0 To filas
                Dim obj As New OrdenCompraDetalleBE
                lst.Add(obj)
            Next

        End If

        Return lst

    End Function

    Private Function ObtenerProducto(ByVal codigo As Integer) As ProductoBE

        Dim res As New RespuestaBE
        Dim obj As New ProductoBE

        res = New ProductoBL().listarProductoxCodigo(codigo)

        If res.Respuesta Then
            obj = CType(res.ValorRertono, ProductoBE)
        End If

    End Function

    Private Sub ListarParametricaXPadre(ByVal codigoPadre As Integer)

        Dim res As New RespuestaBE
        Dim lst As New List(Of ParametricaBE)

        res = New ParametricaBL().ListarParametricaXPadre(codigoPadre)

        If res.Respuesta Then
            cmbTipoComprobante.DataSource = New BaseForm().CargarCombo(res)
            SelectItem(cmbTipoComprobante, "")
        End If

    End Sub

    Friend Sub SetearProveedor(ByVal codigo As String, descripcion As String)

        txtCodigoProveedor.Text = codigo
        txtDescripcionProveedor.Text = descripcion

    End Sub

    Private Function Guardar() As RespuestaBE

        Dim obj As New OrdenCompraBE

        obj.CORD_NUM_ORDEN = txtOrdenCompra.Text
        obj.NEMP_CODIGO = 1
        obj.NREP_CODIGO = 1 'txtCodigoRepresentante.Text
        obj.DORD_FECHA = CDate(dtpFechaCompra.Value)
        obj.CORD_TIP_COMP = cmbTipoComprobante.SelectedValue
        obj.CORD_NUM_COMP = txtComprobante.Text
        obj.CORD_SUBTOTAL = Val(txtSubTotal.Text)
        obj.CORD_IGV = Val(txtIGV.Text)
        obj.CORD_TOTAL = Val(txtTotal.Text)

        Return New OrdenCompraBL().Guardar(obj, usuario)

    End Function


#End Region

#Region "Evento de Controles"

    Public Overrides Sub tsbGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Guardar()
    End Sub

    Private Sub btnProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProveedor.Click

        Dim frm As New frmBusProveedor(Me)
        frm.Owner = Me
        frm.ShowDialog()

        If (frm.DialogResult = Windows.Forms.DialogResult.OK) Then
            txtCodigoProveedor.Text = frm.vRespuesta.PARAMETRO01
            txtDescripcionProveedor.Text = frm.vRespuesta.PARAMETRO02
        End If

    End Sub

    Private Sub btnRepresentante_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRepresentante.Click

        Dim frm As New frmBuscarRepresentante(Me)
        frm.Owner = Me
        frm.ShowDialog()

        If (frm.DialogResult = Windows.Forms.DialogResult.OK) Then
            txtCodigoProveedor.Text = frm.vRespuesta.PARAMETRO01
            txtDescripcionProveedor.Text = frm.vRespuesta.PARAMETRO02
        End If

    End Sub

#End Region

#Region "Otros"

    'Dim dt As New Data.DataTable
    'dt.Columns.Add("Codigo")
    'dt.Columns.Add("Descripcion")

    'Dim row As Data.DataRow
    'row = dt.NewRow()
    'row.Item("Codigo") = "001"
    'row.Item("Descripcion") = "Arroz"
    'dt.Rows.Add(row)

    'row = dt.NewRow()
    'row.Item("Codigo") = "002"
    'row.Item("Descripcion") = "Aceite"
    'dt.Rows.Add(row)

    'row = dt.NewRow()
    'row.Item("Codigo") = "003"
    'row.Item("Descripcion") = "Azucar"
    'dt.Rows.Add(row)

    'Dim cmb As DataGridViewComboBoxColumn = DirectCast(grdLista.Columns(1), DataGridViewComboBoxColumn)

    'cmb.DataSource = dt
    'cmb.ValueMember = "Codigo"
    'cmb.DisplayMember = "Descripcion"

#End Region

#Region "Eventos GrdiView"

    Private Sub grdLista_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdLista.CellContentClick

        MsgBox("Estado: " & Me.grdLista.Rows(e.RowIndex).Cells(0).Value)

    End Sub

    Private Sub grdLista_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdLista.CellValueChanged

        If Me.grdLista.Rows(e.RowIndex).Cells("grdCodigoProducto").ColumnIndex Then
            Me.grdLista.Rows(e.RowIndex).Cells("grdProducto").Value = "La Torre"
        End If
        'MsgBox("Estado: " & Me.grdLista.Rows(e.RowIndex).Cells("grdCodigoProducto").Value)

    End Sub

    Private Sub grdLista_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles grdLista.KeyDown

        Dim row As Integer = grdLista.CurrentRow.Index
        Dim cell As DataGridViewCell
        cell = grdLista.Rows(grdLista.CurrentRow.Index).Cells(grdLista.CurrentCell.ColumnIndex)

        If cell.Selected Then

            Select Case grdLista.CurrentCell.ColumnIndex

                Case 2, 3

                    If Asc(e.KeyValue).ToString = Asc(Keys.F2).ToString Then

                        Dim frm As New frmBusProveedor(Me)
                        frm.Owner = Me
                        frm.ShowDialog()

                        If (frm.DialogResult = Windows.Forms.DialogResult.OK) Then
                            grdLista.Rows(row).Cells(2).Value = frm.vRespuesta.PARAMETRO01
                            grdLista.Rows(row).Cells(3).Value = frm.vRespuesta.PARAMETRO02
                        End If

                    End If

                Case 4

                    If Asc(e.KeyValue).ToString = Asc(Keys.F2).ToString Then

                        Dim frm As New frmBuscarRepresentante(Me)
                        frm.Owner = Me
                        frm.ShowDialog()

                        If (frm.DialogResult = Windows.Forms.DialogResult.OK) Then
                            grdLista.Rows(row).Cells(2).Value = frm.vRespuesta.PARAMETRO01
                            grdLista.Rows(row).Cells(3).Value = frm.vRespuesta.PARAMETRO02
                        End If

                    End If

            End Select

        End If

    End Sub

#End Region

End Class